const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    devtool: 'inline-source-map',
    /*入口*/
    entry: {
        app: [
        'react-hot-loader/patch',
        path.join(__dirname, 'src/index.js')
        ],
        vendor: ['react', 'react-router-dom', 'redux', 'react-dom', 'react-redux']
    },
    
    /*输出到dist文件夹，输出文件名字为bundle.js*/
    output: {
        path: path.join(__dirname, './dist'),
        filename: '[name].[hash].js',
        chunkFilename: '[name].[chunkhash].js'
    },
     /*src文件夹下面的以.js结尾的文件，要使用babel解析*/
	 /*cacheDirectory是用来缓存编译结果，下次编译加速*/
	module: {
		rules: [
            {
    			test: /\.js$/,
    			use: ['babel-loader?cacheDirectory=true'],
    			include: path.join(__dirname, 'src')
    		},
            // 样式
            {
               test: /\.css$/,
               use: ['style-loader', 'css-loader']
            },
            // 图片
            {
                test: /\.(png|jpg|gif)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 8192 //这里是8K
                    }
                }]
            }
        ]
	},
    // 本地路径别名化 
    resolve: {
        alias: {
            pages: path.join(__dirname, 'src/pages'),
            component: path.join(__dirname, 'src/component'),
            router: path.join(__dirname, 'src/router'),
            actions: path.join(__dirname, 'src/redux/actions'),
            reducers: path.join(__dirname, 'src/redux/reducers'),
            // redux: path.join(__dirname, 'src/redux')

        }
    },
	// 本地服务
	devServer: {
		// contentBase url根目录
        contentBase: path.join(__dirname, './dist'),
     	port: 8081,
        historyApiFallback: true,
        host: '0.0.0.0',
        hot: true
    },
    plugins:[
        // 热更新
        new webpack.HotModuleReplacementPlugin(),
        // 根据模块建立INDEX页面
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: path.join(__dirname, 'src/index.html')
        }),
        // 抽离的公共文件
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor'
        })
    ]
};